package com.yb.web.shoper;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.yb.base.web.BaseAction;
import com.yb.bussiness.goods.CategoryService;
import com.yb.bussiness.goods.CategoryServiceImpl;
import com.yb.bussiness.goods.GoodsService;
import com.yb.bussiness.goods.GoodsServiceImpl;
import com.yb.bussiness.shoper.BrandService;
import com.yb.bussiness.shoper.BrandServiceImpl;
import com.yb.bussiness.shoper.TgBrandMemberService;
import com.yb.bussiness.shoper.TgBrandMemberServiceImpl;
import com.yb.bussiness.shoper.TgPingJiaService;
import com.yb.bussiness.shoper.TgPingJiaServiceImpl;
import com.yb.domain.goods.TgBrand;
import com.yb.domain.goods.TgBrandMember;
import com.yb.domain.goods.TgCategory;
import com.yb.domain.goods.TgGoods;
import com.yb.domain.member.TsSysuser;
import com.yb.util.comm.Contants;
import com.yb.util.comm.RequestUtil;
import com.yb.util.framework.PageUtil;

public class TgBrandMemberAction extends BaseAction {
	private TgBrandMemberService service;
	private BrandService brandService;
	private GoodsService goodService;
	private int total;
	private TgBrandMember tgBrandMember;
	private CategoryService categoryService;
	private  List<TgCategory> catrows;
	private TgPingJiaService pingJiaService;
	
	@SuppressWarnings("unchecked")
	private List rows;

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public List getRows() {
		return rows;
	}

	public void setRows(List rows) {
		this.rows = rows;
	}

	public TgBrandMemberAction() {
		service = new TgBrandMemberServiceImpl();
		goodService = new GoodsServiceImpl();
		categoryService = new CategoryServiceImpl();
		brandService = new BrandServiceImpl();
		pingJiaService = new TgPingJiaServiceImpl();
	}

	/**
	 * 查询
	 * 
	 * @return
	 */
	public String search() {
		String page = RequestUtil.getParam(request, "page", "1");
		String row = RequestUtil.getParam(request, "rows", "10");
		String createTime_startTime = RequestUtil.getParam(request, "createTime_startTime", "");
		String createTime_endTime = RequestUtil.getParam(request, "createTime_endTime", "");
		String partTime_startTime = RequestUtil.getParam(request, "partTime_startTime", "");
		String partTime_endTime = RequestUtil.getParam(request, "partTime_endTime", "");
		String categoryId = RequestUtil.getParam(request, "categoryId", "");
		String bigCategory = RequestUtil.getParam(request, "bigCategory", "");
		String brandId = RequestUtil.getParam(request, "brandId", "");
		String autding = RequestUtil.getParam(request, "autding", "");
		Map<String, String> parameMap = new HashMap<String, String>();
		TsSysuser tsSysuser = (TsSysuser) RequestUtil.getLoginUserFromSession(
				request, Contants.SESSION_USER);
		if ("3".equals(tsSysuser.getType())) {
			parameMap.put("userid", tsSysuser.getId().toString());
		}
		parameMap.put("page", page);
		parameMap.put("row", row);
		parameMap.put("createTime_startTime", createTime_startTime);
		parameMap.put("createTime_endTime", createTime_endTime);
		parameMap.put("partTime_startTime", partTime_startTime);
		parameMap.put("partTime_endTime", partTime_endTime);
		parameMap.put("categoryId", categoryId);
		parameMap.put("bigCategory", bigCategory);
		parameMap.put("brandid", brandId);
		parameMap.put("autding", autding);
		
		int count = service.count1(parameMap);
		if (count != 0) {
			total = count;
			List<TgBrandMember> aclist = service.findByParameMap1(parameMap);
			rows = aclist;
		}
		return Contants.JSONSTRING_RESULT_NAME;

	}

	 public String loadremind(){
	        PrintWriter out;
	        try{
	            Map<String, String> parameMap = new HashMap<String, String>();
	            parameMap.put("autding", "0");
	            response.reset();
	            response.setCharacterEncoding("utf-8");
	            out = response.getWriter();
	            Integer count = service.count1(parameMap);
	            out.write(count+"");
	        }catch (Exception e) {
	            log.error("加载新增的申请", e);
	        }
	        return null;
	    }
	
	
	/**
	 * 显示拼购信息列表出错
	 * 
	 * @return
	 */
	public String pingqiangrecord() {
		try {
			catrows = categoryService.findAll(1, 50);
		} catch (Exception e) {
			log.error("显示拼购记录管理出错", e);
		}
		return "pingqiangrecord";
	}
	/**
	 * 删除
	 * 
	 * @return
	 */
	public String deleteById() {
		PrintWriter out = null;
		try {
			out = response.getWriter();
			String idStr = RequestUtil.getParam(request, "ids", "");
			boolean isboolean = service.deleteById(idStr);
			out.println("{result:" + isboolean + "}");
		} catch (IOException e) {
			e.printStackTrace();
		}

		return null;
	}

	/**
	 * 审核
	 * 
	 * @return
	 */
	public String updateMessageById() {
		PrintWriter out = null;
		try {
			out = response.getWriter();
			String idStr = RequestUtil.getParam(request, "id", "");
			String autding = RequestUtil.getParam(request, "autding", "");
			boolean isboolean = service.updateAutingId(idStr, autding);
			out.println("{result:" + isboolean + "}");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 申请抢购
	 * 
	 * @return
	 */
	public String saveOrUpdate() {
		try {
			tgBrandMember.setName(URLDecoder.decode(tgBrandMember.getName(),"utf-8"));
			tgBrandMember.setCreateTime(new Date());
			tgBrandMember.setAddress(URLDecoder.decode(tgBrandMember
					.getAddress(), "utf-8"));
			TsSysuser tsSysuser = (TsSysuser) RequestUtil.getLoginUserFromSession(
                request, Contants.SESSION_USER);
			//if(tsSysuser==null)tgBrandMember.setType("1");
			boolean isboolean = service.saveOrUpdate(tgBrandMember);
			if (isboolean)
				errorMessage = "1";
			else
				errorMessage = "2";
			response.getWriter().print(errorMessage);
		} catch (Exception e) {
			e.printStackTrace();
			log.error(" TgBrandMemberAction saveOrUpdate error ", e);
			try {
				response.getWriter().print("2");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 
	 * @return
	 */
	public String pingQiang() {
		String id = RequestUtil.getParam(request, "id", "");
		TgGoods tgood = goodService.read(Long.parseLong(id));
		TsSysuser tsSysuser = (TsSysuser) RequestUtil.getLoginUserFromSession(request, Contants.SESSION_USER);
		if (null != tgood) {
			//验证改业主是否已经参加过拼购
			if(null!=tsSysuser){
				Map<String, String> parameMap = new HashMap<String, String>();
				parameMap.put("autding", "1");
				parameMap.put("brandid", id);
				List<TgBrandMember> aclist = service.findByParameMap1(parameMap);
				ActionContext.getContext().put("joinQiangouFlag", 0);
				if(null!=aclist&&aclist.size()>0)
				for (int i = 0; i < aclist.size(); i++) {
					if(aclist.get(i).getUserId()==tsSysuser.getId()){
						ActionContext.getContext().put("joinQiangouFlag", 1);
						break;
					}
				}
			}
			PageUtil pjPu = pingJiaService.findPjByBrandId(tgood.getBrandId(),"2", 1, 12);
			ActionContext.getContext().put("pjPu", pjPu);
			if(null!=tsSysuser){
				int isPingjia = pingJiaService.isPingjia(tgood.getBrandId(),tsSysuser.getId());
				ActionContext.getContext().put("isPingjia", isPingjia);
			}
			int count = service.searchCountByBrandId(tgood.getId().toString());
			Map<String, String> parameMap = new HashMap<String, String>();
//			parameMap.put("autding", "1");
			parameMap.put("brandid", tgood.getId().toString());
			List<TgBrandMember> memberlist = service.findByParameMap1(parameMap);
			// 本月拼抢人数
			ActionContext.getContext().put("count", count);
			// 本月已拼人
			ActionContext.getContext().put("goodsmemberlist", memberlist);
			// 该商品属于哪个品牌
			TgBrand tg = brandService.findById(tgood.getBrandId()+"");
			if (tg != null) {
				tgood.setBrandName(tg.getName());
				TgCategory chilcategory = categoryService.findById(Long
						.parseLong(tg.getCategoryId().toString()));
				if (chilcategory != null) {
					tgood.setSecondBrandName(chilcategory.getName());
					TgCategory frisechilcategory = categoryService
							.findById(Long.parseLong(chilcategory.getPid()
									.toString()));
					if (frisechilcategory == null) {

						tgood.setFirstBrandName(chilcategory.getName());
						tgood.setSecondBrandName("");
					} else {
						tgood.setFirstBrandName(frisechilcategory.getName());
					}
				}
			}
		}
		ActionContext.getContext().put("tgood", tgood);
		Long categoryId = RequestUtil.getParam(request, "categoryId", 0l);
		TgCategory tg = categoryService.findById(categoryId);
		ActionContext.getContext().put("category", tg);
		if (tg.getPid() > 0) {
			TgCategory ptg = categoryService.findById(new Long(tg.getPid()));
			ActionContext.getContext().put("pcategory", ptg);
		}
		return "showgoods";
	}

	public TgBrandMemberService getService() {
		return service;
	}

	public void setService(TgBrandMemberService service) {
		this.service = service;
	}

	public TgBrandMember getTgBrandMember() {
		return tgBrandMember;
	}

	public void setTgBrandMember(TgBrandMember tgBrandMember) {
		this.tgBrandMember = tgBrandMember;
	}

	public List<TgCategory> getCatrows() {
		return catrows;
	}

	public void setCatrows(List<TgCategory> catrows) {
		this.catrows = catrows;
	}
}
